package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xpg.entity.Supplier;
import cn.xpg.service.SupplierService;
import cn.xpg.tools.PageSupport;

@Controller
public class SupplierController {

	@Resource(name = "supplierService")
	private SupplierService supplierService;

	// 分页查询供应商
	@RequestMapping("/getSupplierList.do")
	public String getSupplierList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String supplier_name = request.getParameter("supplier_name");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = supplierService.getSupplierCount(supplier_name);
		System.out.println("查询供应商信息！！！！！！！！！！————>getSupplierList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Supplier> supplierList = null;
		supplierList = supplierService.getSupplierList(supplier_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("supplierList", supplierList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("supplier_name", supplier_name);
			return "/jsp/base/supplier_list";
	}

	// 新增供应商
	@RequestMapping("addSupplier.do")
	public String addSupplier(@Valid Supplier supplier) {
		System.out.println("新增供应商信息！！！！！！！！！！————>addSupplier");
		if (supplierService.addSupplier(supplier) > 0) {
			return "redirect:/getSupplierList.do";
		} else {
			return "/jsp/base/Supplier_list";
		}
	}

	// 获取供应商Id
	@RequestMapping("getSupplierId.do")
	public String getSupplierId(HttpServletRequest request) {
		System.out.println("获取供应商Id！！！！！！！！！！————>getSupplierId");
		int supplier_id = Integer.parseInt(request.getParameter("supplier_id"));
		Supplier supplier =supplierService.getSupplierId(supplier_id);
		request.setAttribute("supplier", supplier);
		return "/jsp/base/supplier_update";
	}

	// 修改供应商
	@RequestMapping("updateSupplier.do")
	public String updateSupplier(@Valid Supplier supplier, HttpServletRequest request) {
		System.out.println("修改供应商信息！！！！！！！！！！————>updateSupplier");
		if (supplierService.updateSupplier(supplier) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getSupplierList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("supplier", supplier);
			return "/jsp/base/supplier_update";
		}
	}

	// 删除供应商
	@RequestMapping("deleteSupplier.do")
	public String deleteSupplier(HttpServletRequest request) {
		System.out.println("删除供应商信息！！！！！！！！！！————>deleteSupplier");
		int supplier_id = Integer.valueOf(request.getParameter("supplier_id"));
		if (supplierService.deleteSupplier(supplier_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getSupplierList.do";
	}
}
